package com.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jd.config.ThresholdProperties;
import com.jd.domain.Product;
import com.jd.domain.PurchaseApply;
import com.jd.domain.Stock;
import com.jd.domain.StockWarningLog;
import com.jd.dto.StockDto;
import com.jd.enums.GlobalErrorCode;
import com.jd.exception.GlobalCustomException;
import com.jd.feign.ProductFeign;
import com.jd.feign.PurchaseFeign;
import com.jd.mapper.StockMapper;
import com.jd.result.JSONResult;
import com.jd.service.IStockService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jd.service.IStockWarningLogService;
import com.jd.until.AssertUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-09-29
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {


    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private IStockWarningLogService stockWarningLogService;
    @Autowired
    private PurchaseFeign purchaseFeign;
    @Autowired
    private ThresholdProperties thresholdProperties;
    @Override
    public void inOut(StockDto stockDto) {
        Stock stock = selectStock(stockDto.getProductId());
        if (stockDto.getState()==0){//入库
            if (stock==null){ //判断是否有这类产品，没有就添加
               stock = new Stock();
               stock.setCount(stockDto.getCount());
               stock.setProductId(stockDto.getProductId());
               //
               JSONResult jsonResult = productFeign.get(stockDto.getProductId());
               //将对象转化为js格式
               Object data = jsonResult.getData();
                String s = JSONObject.toJSONString(data);
                Product product = JSON.parseObject(s, Product.class);
                AssertUtil.isTrue(jsonResult.isSuccess(), GlobalErrorCode.ERROR);
                stock.setProductName(product.getProductName());
                stock.setCreateTime(new Date());
                insert(stock);
            }else { // 商品不为空，说明有加入库
                stock.setCount(stock.getCount()+stockDto.getCount());
                updateById(stock);
            }
        }else {//出库
            //判断库存是否充足
            if (stock.getCount()<thresholdProperties.getKu()){//不足
                StockWarningLog stockWarningLog = new StockWarningLog();
                BeanUtils.copyProperties(stock,stockWarningLog);
                stockWarningLog.setInfo(stock.getProductName()+"库存不足");
                stockWarningLog.setCreateTime(new Date());
                stockWarningLogService.insert(stockWarningLog);
                //库存不足调用采购
                PurchaseApply purchaseApply = new PurchaseApply();
                BeanUtils.copyProperties(stock,purchaseApply);
                purchaseApply.setCount(100);
                purchaseApply.setCreateTime(new Date());
                purchaseFeign.saveOrUpdate(purchaseApply);
                throw new GlobalCustomException("库存不足，出库失败！");
            }else {
                stock.setCount(stock.getCount()-stockDto.getCount());
                updateById(stock);
            }
        }
    }

    @Override
    public List<Stock> lack() {
        Long number = thresholdProperties.getKu();
        Wrapper<Stock> wrapper = new EntityWrapper<>();
        wrapper.lt("count",number);
        List<Stock> stocks = selectList(wrapper);
        return stocks;
    }

    private Stock selectStock(Long productId) {
        Wrapper<Stock> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",productId);
        return selectOne(wrapper);
    }
}
